package main

import (
	"fmt"
	"google.golang.org/grpc"
	"io"
	"Gocode/study.com/RPCandgRPC/grpc/clientstream/model"
	"net"
)

type Person struct {
}

func (u *Person) AddUser(stream model.AddUserService_AddUserServer) error {
	for {
		recvData, err :=stream.Recv()
		if err!=nil{
			fmt.Println("server stream.Recv error=", err)
			return err
		}
		if err == io.EOF{
			fmt.Println("读取结束")
			ret := model.User{Age: 0}
			return stream.SendAndClose(&ret)
		}
		fmt.Println(recvData)
	}
}

func main() {
	//初始化为指针类型
	//person := &Person{}
	//将服务器对象注册
	server := grpc.NewServer()
	//models.RegisterGetUserInfoServiceServer(server, reflect.ValueOf(person).Interface().(models.GetUserInfoServiceServer))
	model.RegisterAddUserServiceServer(server, &Person{})

	//监听
	listen, err := net.Listen("tcp", ":9000")
	if err != nil {
		fmt.Println("net.Listen error=", err)
		//return
	}
	// 开启服务
	server.Serve(listen)

}
